package handlers

import (
	"github.com/rs/xid"
	"github.com/shugen/models"
	"github.com/shugen/utils"
	"net/http"
	"strconv"
	"time"
)

//门星关系
func (c *Controller) IMenxing(w http.ResponseWriter, r *http.Request) {

	if r.Method != "POST" {
		return
	}

	type_value, _ := strconv.Atoi(r.PostFormValue("type"))
	menxing := &models.MenXing{
		MenXingId: []byte(xid.New().String()),
		Type:      type_value,
		Title:     r.PostFormValue("title"),
		Desc:      r.PostFormValue("desc"),
		Status:    1,
		CreatedAt: time.Now().UTC(),
		UpdatedAt: time.Now().UTC(),
	}
	if err := c.Svc.InsertMenXing(menxing); err != nil {
		w.Write(utils.Fail(err))
		return
	}
	w.Write(utils.Success(menxing))

}

//update 门神关系
func (c *Controller) UMenxing(w http.ResponseWriter, r *http.Request) {

	mx := &models.MenXing{
		MenXingId: []byte(r.PostFormValue("MenXingId")),
		//Type:      0,
		Title:  r.PostFormValue("title"),
		Desc:   r.PostFormValue("desc"),
		Status: 0,
		//CreatedAt: time.Time{},
		UpdatedAt: time.Now().UTC(),
	}

	menshen, err := c.Svc.UpdateMenXing(mx)

	if err != nil {
		w.Write(utils.Fail(err))
		return
	}
	w.Write(utils.Success(menshen))
}

//select 门星关系
func (c *Controller) SMenxing(w http.ResponseWriter, r *http.Request) {

	mx, err := c.Svc.SelectMenXing(&models.MenXing{MenXingId: []byte(r.PostFormValue("MenXingId"))})

	if err != nil {
		w.Write(utils.Fail(err))
		return
	}
	w.Write(utils.Success(mx))
}

//select List 门星关系
func (c *Controller) LMenxing(w http.ResponseWriter, r *http.Request) {

	mx, err := c.Svc.ListMenXing()

	if err != nil {
		w.Write(utils.Fail(err))
		return
	}
	w.Write(utils.Success(mx))
}
